layui.use(['form', 'laydate'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;
    var laydate = layui.laydate;


    //日期
    laydate.render({
        elem: '#enableTime'
    });
    laydate.render({
        elem: '#startTime'
    });
    laydate.render({
        elem: '#endTime'
    });


    //获取当前优惠券id
    var id = $("#discountId").val();
    //设置获取当前优惠券的数据的路径
    var url1 = ctx + "/discount/discount_update_data?id=" + id;
    // 使用jQuery的$.post()方法获取后台数据
    $.post(url1, function (data) {
        //渲染表格数据
        form.val("demo-val-filter",data);

        //设置领取时间
        lay('#enableTime').each(function () {
            laydate.render({
                elem: this,
                trigger: 'click',
                value: data.enableTime
            });
        });
        //设置开始时间
        lay('#startTime').each(function () {
            laydate.render({
                elem: this,
                trigger: 'click',
                value: data.startTime
            });
        });
        //设置结束时间
        lay('#endTime').each(function () {
            laydate.render({
                elem: this,
                trigger: 'click',
                value: data.endTime
            });
        });
    });


    // 提交事件
    form.on('submit(submit_val)', function (data) {
        var field = data.field; // 获取表单字段值
        console.log(field.count);
        console.log(field.minPoint);
        // 请求的地址
        var url = ctx + "/discount/discountUpdate"; // 编辑操作
        layer.confirm('确定提交吗？', function (index) {
            layer.close(index); // 关闭确认提示框
            $.ajax({
                type: "post",
                url: url,
                data: {
                    id:id,
                    type: field.type,
                    name: field.name,
                    platform: field.platform,
                    count: field.count,
                    amount: field.amount,
                    perLimit: field.perLimit,
                    minPoint: field.minPoint,
                    enableTime: field.enableTime,
                    startTime: field.startTime,
                    endTime: field.endTime,
                    useType: field.useType
                },
                success: function (result) {
                    // 判断是否添加成功
                    if (result.code == 200) {
                        // 优惠券添加成功，跳转到优惠券查询页面
                        layer.msg("优惠券信息修改成功，系统将在3秒钟跳转到优惠券管理页面", function () {
                            // 关闭加载层
                            top.layer.close(index);
                            // 关闭弹出层
                            layer.closeAll("iframe");
                            // 刷新父窗口，重新加载数据
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 5});
                    }
                }
            })
        });
        return false;
    });

});